/*
 * Indexer.java
 *
 * Created on 6 March 2006, 13:05
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
//import model.*;
package lucene;

import java.io.IOException;
import java.io.StringReader;
import java.io.File;
import java.util.List;
import model.Phone;
import model.PhoneStore;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class Indexer {

    /**
     * Creates a new instance of Indexer
     */
    public Indexer() {
        //indexWriter.setRAMBufferSizeMB(256);
    }

    private IndexWriter indexWriter = null;

    public IndexWriter getIndexWriter(boolean create) throws IOException {
        if (indexWriter == null) {
            Directory indexDir = FSDirectory.open(new File("src/indexDirectory"));
            IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_2, new StandardAnalyzer());
            indexWriter = new IndexWriter(indexDir, config);
        }
        return indexWriter;
    }

    public void closeIndexWriter() throws IOException {
        if (indexWriter != null) {
            indexWriter.close();
        }
    }

    public void initDirectory(File path) {

        if (path.exists()) {
            File[] files = path.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    initDirectory(files[i]);
                } else {
                    files[i].delete();
                }
            }
        }
    }

    public void indexSong(Phone phone) throws IOException {

        //System.out.println("Indexing hotel: " + song);
        IndexWriter writer = getIndexWriter(false);
        Document doc = new Document();
        //doc.add(new StringField("id", hotel.getId(), Field.Store.YES));
        
        writer.addDocument(doc);
    }

    public void rebuildIndexes(PhoneStore phoneStore) throws IOException {
        //
        // Erase existing index
        //
        File path = new File("src/indexDirectory");
        initDirectory(path);
        getIndexWriter(true);
        //
        // Index all Accommodation entries
        //
        List<Phone> db = phoneStore.db;
        for (int i = 0; i < db.size(); i++) {
            indexSong(db.get(i));
        }
        //
        // Don't forget to close the index writer when done
        //
        closeIndexWriter();
    }

    public void rebuildIndexes(String XMLpath) throws IOException {
        File path = new File("indexDirectory");
        initDirectory(path);
        //
        //
        ReadXML readXML = new ReadXML(XMLpath);
        PhoneStore phoneStore = readXML.getPhoneStore();
        //
        //
        getIndexWriter(true);
        //
        // Index all Accommodation entries
        //
        List<Phone> db = phoneStore.db;
        for (int i = 0; i < db.size(); i++) {
            indexSong(db.get(i));
        }
        //
        // Don't forget to close the index writer when done
        //
        closeIndexWriter();
        System.out.println("Rebuild Success");
    }

}
